package com.projecy.product.controller;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.projecy.product.dto.ProductDto;
import com.projecy.product.config.Number;
import com.projecy.product.model.Drug;
import com.projecy.product.model.ProductFile;
import com.projecy.product.dto.PageDto;
import com.projecy.product.service.DrugSerice;
import com.projecy.product.service.ProductFileService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import static com.projecy.common.utils.ResultUtil.success;

@RequestMapping("/sys")
@RestController
public class ProductFileController {
    @Autowired
    private ProductFileService productFileService;
    @Autowired
    private DrugSerice drugSerice;

    /*根据点击事件进行药品的查询*/
    @GetMapping("/drug/select/{drugName}")
    public List<Drug> selectByDrugName(@PathVariable String drugName){
        QueryWrapper<Drug> wrapper = new QueryWrapper<>();
        wrapper.eq("drugName",drugName);
        List<Drug> list = drugSerice.list(wrapper);
        return list;
    }

    /*点击产品档案登记进行产品的添加*/
    @PostMapping("/product/add")
    public ResponseEntity<Object> productInsert(@RequestBody ProductDto productDto){
        Long didStr = Number.prdouctStock();
        productDto.setProductId(didStr.toString());
        Boolean bool = productFileService.saveProductFile(productDto);
        return success(bool,null);
    }

    /*点击产品档案登记复核进行未复核的数据的查询*/
    @GetMapping("/product/selectNotAuditMark")
    public List<ProductFile> selectNotAuditMark(){
        QueryWrapper<ProductFile> wrapper = new QueryWrapper<>();
        wrapper.eq("audit_mark","未审核");
        return productFileService.list(wrapper);
    }

    /*点击查询出所有已审核的数据*/
    @GetMapping("/product/productAuditMark")
    public List<ProductFile> selectAuditMark(){
        QueryWrapper<ProductFile> wrapper = new QueryWrapper<>();
        wrapper.eq("audit_mark","已审核");
        return productFileService.list(wrapper);
    }

    /*点击复核按钮根据id进行复核*/
    @GetMapping("/product/productAuditMark/{pid}")
    public Boolean productAuditMark(@PathVariable int pid){
        UpdateWrapper<ProductFile> wrapper = new UpdateWrapper<>();
        wrapper.set("audit_mark","已审核");
        wrapper.eq("pid",pid);
        return productFileService.update(wrapper);
    }

    @GetMapping("/product/selectPage")
    public IPage<ProductFile> selectAuditMark(@RequestBody PageDto pageDto){
        Page p=new Page();
        QueryWrapper<ProductFile> wrapper = new QueryWrapper<>();
        wrapper.eq("audit_mark","已审核");
        wrapper.eq(!ObjectUtil.isEmpty(pageDto.getProductId()),"product_id",pageDto.getProductId());
        wrapper.eq(!ObjectUtil.isEmpty(pageDto.getProductName()),"product_name",pageDto.getProductName());
        wrapper.eq(!ObjectUtil.isEmpty(pageDto.getType()),"type",pageDto.getType());
        p.setCurrent(pageDto.getCurrent());
        p.setSize(pageDto.getSize());
        return productFileService.page(p,wrapper);
    }

    @PostMapping("/product/updateProduct")
        public boolean updateProduct(@RequestBody ProductFile productFile){
            UpdateWrapper<ProductFile> wrapper = new UpdateWrapper<>();
            wrapper
                    .eq("pid",productFile.getPid())
                    .set("changes",productFile.getChanges()+1)
                    .set("audit_mark","未审核")
                    .set("product_nikck",productFile.getProductNikck());

        return productFileService.update(wrapper);
    }

    /*点击产品档案删除查询出未删除的数据*/
    @GetMapping("/product/productNotByDelete")
    public List<ProductFile> productNotByDelete(){
        QueryWrapper<ProductFile> wrapper = new QueryWrapper<>();
        wrapper.eq("delete_mark","未删除");
        return productFileService.list(wrapper);
    }

    /*点击删除按钮将未删除的数据根据Id改为已删除*/
    @GetMapping("/product/productNotByDelete/{pid}")
    public String productByDelete(@PathVariable int pid){
        UpdateWrapper<ProductFile> wrapper = new UpdateWrapper<>();
        wrapper.set("delete_mark","已删除");
        wrapper.eq("pid",pid);
        boolean update = productFileService.update(wrapper);
        return "main";
    }

    /*点击档案恢复查询出已删除的数据*/
    @GetMapping("/product/productSeleteByDelete")
    public List<ProductFile> productSeleteByDelete(){
        QueryWrapper<ProductFile> wrapper = new QueryWrapper<>();
        wrapper.eq("delete_mark","已删除");
        return productFileService.list(wrapper);
    }

    /*点击恢复按钮将已删除的数据根据Id改为未删除*/
    @GetMapping("/product/productSeleteByDelete/{pid}")
    public boolean productSeleteNotByDelete(@PathVariable int pid){
        UpdateWrapper<ProductFile> wrapper = new UpdateWrapper<>();
        wrapper.set("delete_mark","未删除");
        wrapper.eq("pid",pid);
        return productFileService.update(wrapper);
    }

    /*点击产品档案永久根据id进行删除*/
    @GetMapping("/product/deleteById/{pid}")
    public boolean deleteById(@PathVariable int pid){
        return productFileService.removeById(pid);
    }
}
